/**
 * 下注、棄牌按鈕
 * @author LuoDong
 * @email luo4521@online.net.tw
 */
package com.addcn.texas.view.components
{	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import com.addcn.common.event.ParamEvent;
		
	public class  BetPanel extends MovieClip
	{
		public static const BET_EVENT : String = "limpEvent";
		private var _limit_chip : int;
		private var _raise : int;
		private var _limp : int;
		private var _my_chip: int;
		private var _max : int;  
		private var _min : int;
		
		public function BetPanel() : void
		{			
			this["RaiseText"].restrict = "0-9";
			var i = 1;
			for (; i <= 7; i++) {
				this["btn" + i].addEventListener(MouseEvent.CLICK, _btnClickHandler);
			}				
			this["RaiseText"].addEventListener(Event.CHANGE, _inputHandler);
			this["LimpBtn"].addEventListener(MouseEvent.CLICK, _betHandler);
			this["FoldBtn"].addEventListener(MouseEvent.CLICK, _betHandler);
			this["RaiseBtn"].addEventListener(MouseEvent.CLICK, _betHandler);
			this["CheckBtn"].addEventListener(MouseEvent.CLICK, _betHandler);
			this["CheckBtn"].visible = false;
		}
		
		/**
		 * 设置下注上限、下限
		 * @param	r_data Object {max:int, min:int, limp:int}
		 */
		public function setLimit(r_data : Object) : void {			
			MonsterDebugger.trace("setlimit", r_data);
			
			var _chip : int = r_data["chip"];
			_limp = r_data["limp"];
			_min = r_data["min"];
			_max = r_data["max"];			
			var _data : Object = { };
			if (_limp == 0) { //看牌
				_data["check"] = 0;
				_data["raise"] = _min ;
				this._raise = _data["raise"];
				if (_raise >= _chip) {
					_raise = _chip;
				}
			}else if (_min >= _max) {  
				if (_max >= _chip) {//全下
					_data["allin"] = _chip;
				}else{ //跟注
					_data["limp"] = _max;
				}
			}else {
				if (_min >= _chip) {//全下
					_data["allin"] = _chip;
				}else{ //跟注
					_data["limp"] = _min;
					_data["raise"] = _min * 2;
					this._raise = _data["raise"];
					if (_raise >= _chip) {
						_raise = _chip;
					}
				}
			}			
						
			if (_data["allin"] != null) {
				this["CheckBtn"].visible = false;
				this["LimpBtn"].visible = true;
				this["LimpTxt"].text = String(_data["allin"]);
			}else if (_data["limp"] != null) {
				this._limp = _data["limp"];
				this["CheckBtn"].visible = false;
				this["LimpBtn"].visible = true;
				this["LimpTxt"].text = String(_data["limp"]);
			}else if (_data["check"] != null) {
				this["CheckBtn"].visible = true;
				this["LimpBtn"].visible = false;
			}
			
			if (_data["raise"] != null) {
				this["RaiseBtn"].visible = true;
				this["RaiseText"].visible = true;				
				this["RaiseText"].text = this._raise;
			}else{				
				this["RaiseBtn"].visible = false;
				this["RaiseText"].visible = false;
			}			
		}
				
		private function _betHandler(evt : MouseEvent) : void
		{
			var _obj : Object = { };
			this._raise = parseInt(this["RaiseText"].text);		
			switch(evt.target.name) {
				case "CheckBtn": //看牌
					_obj = { way:4, chip:0 };
					break;
				case "LimpBtn": //跟注
					_obj = {chip:this._limp,way:0 };
					break;
				case "FoldBtn": //弃牌
					_obj = {way:2,chip:0};					
					break;
				case "RaiseBtn": //加注
					if(this._raise < this._min){
						this._raise =  this._min;
					}else if(this._raise > this._max){
						this._raise = this._max;
					}					
					_obj["chip"] = this._raise;
					_obj["way"] = 1;
					break;
			}			
			trace("raise : " + this._raise);
			var _event = new ParamEvent(BetPanel.BET_EVENT, _obj );
			dispatchEvent(_event);
		}		
		
		private function _btnClickHandler(evt : MouseEvent) : void
		{
			var _index = evt.target.name.substr( -1, 1);			
			switch(_index) {
				case "1":
					this._raise = this._raise + 1;
					break;
				case "2":
					this._raise = this._raise + 2;
					break;
				case "3":
					this._raise = this._raise + 5;
					break;
				case "4":
					this._raise = this._raise + 10;
					break;
				case "5":
					this._raise = this._raise + 20;
					break;
				case "6":
					this._raise = this._raise + 50;
					break;
				case "7":
					this._raise = this._raise + 100;
					break;
			}			
			this._chipRaise(this._raise);
		}
				
		private function _chipRaise(r_num : int) : void
		{
			if (this._raise > this._max) {
				this._raise = this._max;
			}else{
				this._raise = r_num;
			}
			this["RaiseText"].text = String(this._raise);			
		}
		
		private function _inputHandler(evt : Event) : void
		{			
			var _val : int = parseInt(this["RaiseText"].text);
			if (_val > (this._max )) {
				this["RaiseText"].text = this._max ;
				this._raise = this._max;
			}else {
				this._raise = _val;
			}
			
		}
		
	}
	
}
